package com.day1Am.mapper.UserMapperImpl;

import com.day1Am.mapper.UserMapper;
import com.day1Am.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author cc
 * @date 2021/8/4 11:34
 * @Description
 */
public class UserMapperImpl implements UserMapper {
    SqlSession sqlSession =null;
    public List<User> findallUser() {
        String resource = "mybatis-config.xml";
        List<User> users  = null;
        try {
            //把配置文件作为流，暂存到InputStream
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //获取工厂，把流对象纳入
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
           //打开此方法
            SqlSession sqlSession = sqlSessionFactory.openSession();
            users = sqlSession.selectList("com.day1Am.mapper.UserMapper.findallUser");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return users;
    }
}
